//
//  YNTwoPeopleChat.m
//  Messenger
//
//  Created by iOS on 15/3/4.
//  Copyright (c) 2015年 YN-APP-iOS. All rights reserved.
//

#import "YNTwoPeopleChat.h"
#import "FMDatabase.h"
#import "FMDatabaseQueue.h"
#import "FMDatabaseAdditions.h"
#import "YNIMTool.h"
#import "YNIMFrameworkMacro.h"

@interface YNTwoPeopleChat ()
@property (nonatomic, strong) NSArray *fields;

@end

@implementation YNTwoPeopleChat

-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
    if (!value) {
        if ([key isEqualToString:@"messageType"]) {
            self.messageType=0;
        }else if ([key isEqualToString:@"isSendMsg"]){
            self.isSendMsg=0;
        }else if ([key isEqualToString:@"isReaded"]){
            self.isReaded=0;
        }else if ([key isEqualToString:@"isSuccess"]){
            self.isSuccess=0;
        }else if ([key isEqualToString:@"masterType"]){
            self.masterType=0;
        }else if ([key isEqualToString:@"messageHeight"]){
            self.messageHeight=0.f;
        }else if ([key isEqualToString:@"voiceState"]){
            self.voiceState=0;
        }else if ([key isEqualToString:@"fileState"]){
            self.fileState=0;
        }
    }
}

+ (NSString *)packId
{
    return @"packId";
}

+ (NSString *)chatUserNo
{
    return @"chatUserNo";
}

+ (NSString *)fileId
{
    return @"fileId";
}

+ (NSString *)messageType
{
    return @"messageType";
}

+ (NSString *)isSendMsg
{
    return @"isSendMsg";
}

+ (NSString *)message
{
    return @"message";
}

+ (NSString *)content
{
    return @"content";
}

+ (NSString *)mTime
{
    return @"mTime";
}

+ (NSString *)isReaded
{
    return @"isReaded";
}

+ (NSString *)isSuccess
{
    return @"isSuccess";
}

+ (NSString *)masterType
{
    return @"masterType";
}

+ (NSString *)messageBody
{
    return @"messageBody";
}

+ (NSString *)messageHeight
{
    return @"messageHeight";
}

+ (NSString *)senderName
{
    return @"senderName";
}

+ (NSString *)fileName
{
    return @"fileName";
}

+ (NSString *)originPhotoUrl
{
    return @"originPhotoUrl";
}

+ (NSString *)fileUrl
{
    return @"fileUrl";
}

+ (NSString *)tableName
{
    return @"twoPeopleChat";
}

+ (NSString *)fileState
{
    return @"fileState";
}

+(NSString *)messageContinuousKey
{
    return @"messageContinuousKey";
}

+(NSString *)isMessageContinuous
{
    return @"isMessageContinuous";
}

+(NSString *)isMessageWithdraw
{
    return @"isMessageWithdraw";
}


- (NSArray *)insertParam
{
    return @[[YNIMTool nilFilter:self.packId],
             [YNIMTool nilFilter:self.chatUserNo],
             [YNIMTool nilFilter:self.fileId],
             [NSNumber numberWithInt:self.messageType],
             [NSNumber numberWithInt:self.isSendMsg],
             [YNIMTool nilFilter:self.message],
             [YNIMTool nilFilter:self.content],
             [YNIMTool nilFilter:self.mTime],
             [NSNumber numberWithInt:self.isReaded],
             [NSNumber numberWithInt:self.isSuccess],
             [NSNumber numberWithInt:self.masterType],
             [YNIMTool nilFilter:self.messageBody],
             [NSNumber numberWithFloat:self.messageHeight],
             [YNIMTool nilFilter:self.senderName],
             [YNIMTool nilFilter:self.fileName],
             [YNIMTool nilFilter:self.originPhotoUrl],
             [YNIMTool nilFilter:self.fileUrl],
             [NSNumber numberWithInteger:self.fileState],
             [YNIMTool nilFilter:self.messageContinuousKey],
             [NSNumber numberWithInteger:self.isMessageContinuous],
             [NSNumber numberWithInt:self.isMessageWithdraw]];
}

+ (BOOL)creatTwoPeopleChatTableInDatabase:(FMDatabase *)db
{
    if ([db tableExists:[YNTwoPeopleChat tableName]]) {
        return YES;
    }
    NSString *creatTwoPeopleChat = [NSString stringWithFormat:@"%@%@(%@ varchar(30) NOT NULL PRIMARY KEY, %@ varchar(30), %@ varchar(100), %@ INTEGER, %@ INTEGER, %@ TEXT, %@ TEXT, %@ varchar(30), %@ INTEGER, %@ INTEGER, %@ INTEGER, %@ varchar(200), %@ FLOAT, %@ varchar(30), %@ varchar(30), %@ varchar(30), %@ varchar(30), %@ INTEGER, %@ INTEGER, %@ INTEGER, %@ INTEGER)",
                                    CREATE_TABLE,
                                    [YNTwoPeopleChat tableName],
                                    [YNTwoPeopleChat packId],
                                    [YNTwoPeopleChat chatUserNo],
                                    [YNTwoPeopleChat fileId],
                                    [YNTwoPeopleChat messageType],
                                    [YNTwoPeopleChat isSendMsg],
                                    [YNTwoPeopleChat message],
                                    [YNTwoPeopleChat content],
                                    [YNTwoPeopleChat mTime],
                                    [YNTwoPeopleChat isReaded],
                                    [YNTwoPeopleChat isSuccess],
                                    [YNTwoPeopleChat masterType],
                                    [YNTwoPeopleChat messageBody],
                                    [YNTwoPeopleChat messageHeight],
                                    [YNTwoPeopleChat senderName],
                                    [YNTwoPeopleChat fileName],
                                    [YNTwoPeopleChat originPhotoUrl],
                                    [YNTwoPeopleChat fileUrl],
                                    [YNTwoPeopleChat fileState],
                                    [YNTwoPeopleChat messageContinuousKey],
                                    [YNTwoPeopleChat isMessageContinuous],
                                    [YNTwoPeopleChat isMessageWithdraw]];
    
    BOOL result = [db executeUpdate:creatTwoPeopleChat];
    return result;
}

/**JC** 在表中新增一个列（字段） **/
+ (BOOL)insertTwoColumnInDatabase:(FMDatabase*)db MessageContinuous:(NSString*)MessageContinuous columnDataType:(NSString*)type{
    if (![db columnExists:MessageContinuous inTableWithName:[YNTwoPeopleChat tableName]]) {
        //列name在表中不存在，所以加入
        BOOL result= [db executeUpdate:[NSString stringWithFormat:@"%@ %@ add %@ %@",ALTER_TABLE,[YNTwoPeopleChat tableName],MessageContinuous,type]];
        return result;
    }
    return YES;
}

/**HX** 根据packId判断数据库是否存在这条消息 **/
+ (BOOL)determineTwoPeopleChatTableWithPackId:(NSString *)packId Database:(FMDatabase *)db
{
    NSString *queryUserNoSql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE %@ LIKE '%@'",
                                [YNTwoPeopleChat tableName],
                                [YNTwoPeopleChat packId],
                                packId];
    FMResultSet *rs = [db executeQuery:queryUserNoSql];
    
    BOOL result = NO;
    while ([rs next])
    {
        result = YES;
    }
    [rs close];
    return result;
}

/**JC** 根据packId获取这条消息 **/
+(NSArray *)queryTwoPeopleChatTableWithPackId:(NSString *)packId Database:(FMDatabase *)db
{
    NSString *queryUserNoSql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE %@ = '%@'",
                                [YNTwoPeopleChat tableName],
                                [YNTwoPeopleChat packId],
                                packId];
    FMResultSet *rs = [db executeQuery:queryUserNoSql];
    
    NSMutableArray *twoPeopleChatByKeywordArray = [NSMutableArray array];
    while ([rs next]) {
        YNTwoPeopleChat * twoPeopleChat = [[YNTwoPeopleChat alloc] init];
        if (![YNIMTool isEmptyDictionary:[rs resultDictionary]]) {
            [twoPeopleChat setValuesForKeysWithDictionary:[rs resultDictionary]];
            [twoPeopleChatByKeywordArray addObject:twoPeopleChat];
        }
    }
    [rs close];
    return twoPeopleChatByKeywordArray;
}


- (BOOL)insertTwoPeopleChatTableInDatabase:(FMDatabase *)db
{
    BOOL isChatMessageExist =  [YNTwoPeopleChat determineTwoPeopleChatTableWithPackId:self.packId Database:db];
    BOOL result=YES;
    
    //messageContinuousKey 取值为最近第一条消息的messageContinuousKey值//
    if([YNIMTool isEmptyString:self.messageContinuousKey])
    {
        NSArray * twoPeopleArray  =[YNTwoPeopleChat queryTwoPeopleChatTopInfoWithUserNo:self.chatUserNo mTime:nil numberCount:1 Database:db];
        if([YNIMTool isEmptyArray:twoPeopleArray])
        {
            //没有消息，key值取新的GUID值
            self.messageContinuousKey = [YNIMTool getGUID];
        }
        else
        {
            YNTwoPeopleChat * twoPeopleChat = twoPeopleArray.firstObject;
            if([YNIMTool isEmptyString:twoPeopleChat.messageContinuousKey])
            {
                //没有消息，key值取新的GUID值
                self.messageContinuousKey = [YNIMTool getGUID];
            }
            else
            {
                self.messageContinuousKey = twoPeopleChat.messageContinuousKey;
            }
            
        }
    }
    //messageContinuousKey 取值为最近第一条消息的messageContinuousKey值//
    
    //不存在本条信息，直接插入
    if (!isChatMessageExist) {
        
        result =  [self insertTwoPeopleChatInDatabase:db];
    }
    //存在这条消息，并且isMessageContinuous是连续YES
    //修改所有的值为messageContinuousKey，isMessageContinuous相同的为最新的key值
    else if(isChatMessageExist == YES && self.isMessageContinuous == YES)
    {
        NSArray * twoPeopleArray = [YNTwoPeopleChat queryTwoPeopleChatTableWithPackId:self.packId Database:db];
        if(![YNIMTool isEmptyArray:twoPeopleArray])
        {
            YNTwoPeopleChat * oldTwopeopleChat = twoPeopleArray.firstObject;
            //1,修改当前数据信息
            result= [YNTwoPeopleChat updateTwoPeopleChatMessageContinuousTableWithPackId:self.packId messageContinuousKey:self.messageContinuousKey isMessageWithdraw:self.isMessageWithdraw inDatabase:db];
            //2，修改所有的messageContinuousKey相同值，isMessageContinuous为YES的数据。
            if(![YNIMTool isEmptyString:oldTwopeopleChat.messageContinuousKey])
            {
                result = [YNTwoPeopleChat updateTwoPeopleChatAllMessageContinuousWithMessageContinuousKey:self.messageContinuousKey oldMessageContinuousKey:oldTwopeopleChat.messageContinuousKey inDatabase:db];
            }
            
        }
    }
    return result;
    
}

-(BOOL)insertTwoPeopleChatInDatabase:(FMDatabase *)db
{
    BOOL result;
    NSString *fieldString = [NSString stringWithFormat:@"%@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@",
                             [YNTwoPeopleChat packId],
                             [YNTwoPeopleChat chatUserNo],
                             [YNTwoPeopleChat fileId],
                             [YNTwoPeopleChat messageType],
                             [YNTwoPeopleChat isSendMsg],
                             [YNTwoPeopleChat message],
                             [YNTwoPeopleChat content],
                             [YNTwoPeopleChat mTime],
                             [YNTwoPeopleChat isReaded],
                             [YNTwoPeopleChat isSuccess],
                             [YNTwoPeopleChat masterType],
                             [YNTwoPeopleChat messageBody],
                             [YNTwoPeopleChat messageHeight],
                             [YNTwoPeopleChat senderName],
                             [YNTwoPeopleChat fileName],
                             [YNTwoPeopleChat originPhotoUrl],
                             [YNTwoPeopleChat fileUrl],
                             [YNTwoPeopleChat fileState],
                             [YNTwoPeopleChat messageContinuousKey],
                             [YNTwoPeopleChat isMessageContinuous],
                             [YNTwoPeopleChat isMessageWithdraw]];
    
    NSString * insertTwoPeopleChatSql = [NSString stringWithFormat:@"%@%@ (%@) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", INSERT_TABLE, [YNTwoPeopleChat tableName], fieldString];
    
    result = [db executeUpdate:insertTwoPeopleChatSql withArgumentsInArray:[self insertParam]];
    return result;
}

+ (BOOL)deleteAllChatInDatabase:(FMDatabase *)db
{
    if (![db tableExists:[YNTwoPeopleChat tableName]]) {
        return YES;
    }
    NSString *deleteAllTwoPeopleChatSql = [NSString stringWithFormat:@"DELETE FROM %@ ", [YNTwoPeopleChat tableName]];
    //删除自增字段数据
    NSString *deleteAutoFieldSql = [NSString stringWithFormat:@"UPDATE sqlite_sequence SET seq = 0 WHERE name='%@'", [YNTwoPeopleChat tableName]];
    
    BOOL result = [db executeUpdate:deleteAllTwoPeopleChatSql];
    
    if (result)
    {
        result = [db executeUpdate:deleteAutoFieldSql];
    }
    
    return result;
}

+ (BOOL)deleteTwoPeopleChatWithUserNo:(NSString *)userNo inDatabase:(FMDatabase *)db
{
    NSString *deleteTwoPeopleChatSql = [NSString stringWithFormat:@"DELETE FROM %@ WHERE %@ = ?", [YNTwoPeopleChat tableName], [YNTwoPeopleChat chatUserNo]];
    
    BOOL result = [db executeUpdate:deleteTwoPeopleChatSql,userNo];
    
    return result;
}

+ (NSString*)queryMinTimeByTwoPeopleChatWithUserNo:(NSString*)userNo inDatabase:(FMDatabase *)db{
    NSString *queryTwoPeopleChatSql;
    queryTwoPeopleChatSql = [NSString stringWithFormat:@"SELECT MIN(%@) FROM %@ WHERE %@ = '%@'",
                             [YNTwoPeopleChat mTime],
                             [YNTwoPeopleChat tableName],
                             [YNTwoPeopleChat chatUserNo],
                             userNo
                             ];
    NSString *minTimeString = @"";
    FMResultSet *rs = [db executeQuery:queryTwoPeopleChatSql];
    while ([rs next]) {
        if (![YNIMTool isEmptyDictionary:[rs resultDictionary]]) {
            if (![YNIMTool isEmptyString:[[rs resultDictionary] objectForKey:@"MIN(mTime)"]]) {
                minTimeString=[[rs resultDictionary] objectForKey:@"MIN(mTime)"];
            }
        }
    }
    [rs close];
    return minTimeString;
}

/**
 根据userNo查询当前数据
 
 @param userNo
 @param db
 @return
 */
+(NSArray*)queryTwoPeopleChatWithUserNo:(NSString*)userNo inDatabase:(FMDatabase *)db
{
    NSString *queryTwoPeopleChatByKeywordSql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE  %@ = '%@'",
                                                [YNTwoPeopleChat tableName],
                                                [YNTwoPeopleChat chatUserNo],
                                                userNo];
    FMResultSet *rs = [db executeQuery:queryTwoPeopleChatByKeywordSql];
    NSMutableArray *twoPeopleChatByKeywordArray = [NSMutableArray array];
    
    while ([rs next]) {
        
        YNTwoPeopleChat * twoPeopleChat = [[YNTwoPeopleChat alloc] init];
        if (![YNIMTool isEmptyDictionary:[rs resultDictionary]]) {
            [twoPeopleChat setValuesForKeysWithDictionary:[rs resultDictionary]];
        }
        
        [twoPeopleChatByKeywordArray addObject:twoPeopleChat];
    }
    [rs close];
    
    return twoPeopleChatByKeywordArray;
}

+ (NSArray *)queryTwoPeopleChatWithUserNo:(NSString *)userNo startIndex:(NSInteger)start endIndex:(NSInteger)end isSelectSendSuccessMessage:(BOOL)selectSuccess inDatabase:(FMDatabase *)db
{
    NSString *OrderByStr = [NSString stringWithFormat:@" ORDER BY %@ DESC",[YNTwoPeopleChat mTime]];
    return [self queryTwoPeopleChatWithUserNo:userNo
                                      orderBy:OrderByStr
                                   startIndex:start
                                     endIndex:end
                   isSelectSendSuccessMessage:selectSuccess
                                   inDatabase:db];
}

+ (NSArray *)queryTwoPeopleChatWithUserNo:(NSString *)userNo orderBy:(NSString *)orderBy startIndex:(NSInteger)start endIndex:(NSInteger)end isSelectSendSuccessMessage:(BOOL)selectSuccess inDatabase:(FMDatabase *)db
{
    NSString *queryTwoPeopleChatSql;
    
    if (selectSuccess)
    {
        queryTwoPeopleChatSql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE 1 = 1 AND %@ = '%@' AND %@ = %@ %@ LIMIT %ld, %ld",
                                 [YNTwoPeopleChat tableName],
                                 [YNTwoPeopleChat chatUserNo],
                                 userNo,
                                 [YNTwoPeopleChat isSuccess],
                                 [NSNumber numberWithBool:selectSuccess],
                                 orderBy,
                                 (long)start,
                                 (long)end];
    }
    else
    {
        queryTwoPeopleChatSql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE 1 = 1 AND %@ = '%@' %@ LIMIT %ld, %ld",
                                 [YNTwoPeopleChat tableName],
                                 [YNTwoPeopleChat chatUserNo],
                                 userNo,
                                 orderBy,
                                 (long)start,
                                 (long)end];
    }
    
    NSMutableArray *twoPeopleChatArray = [NSMutableArray array];
    
    FMResultSet *rs = [db executeQuery:queryTwoPeopleChatSql];
    
    while ([rs next]) {
        
        YNTwoPeopleChat *twoPeopleChat = [[YNTwoPeopleChat alloc] init];
        if (![YNIMTool isEmptyDictionary:[rs resultDictionary]]) {
            [twoPeopleChat setValuesForKeysWithDictionary:[rs resultDictionary]];
        }
        
        [twoPeopleChatArray addObject:twoPeopleChat];
        
    }
    [rs close];
    
    return twoPeopleChatArray;
}

+ (NSArray *)queryTwoPeopleChatByKeywords:(NSString *)keyWords byUserNo:(NSString *)userNo inDatabase:(FMDatabase *)db;

{
    NSString *queryTwoPeopleChatByKeywordSql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE  %@ = '%@' AND %@ LIKE '%%%@%%'escape '/' AND %@ = 0 ORDER BY %@ DESC",
                                                [YNTwoPeopleChat tableName],
                                                [YNTwoPeopleChat chatUserNo],
                                                userNo,
                                                [YNTwoPeopleChat content],
                                                keyWords,
                                                [YNTwoPeopleChat isMessageWithdraw],
                                                [YNTwoPeopleChat mTime]];
    FMResultSet *rs = [db executeQuery:queryTwoPeopleChatByKeywordSql];
    NSMutableArray *twoPeopleChatByKeywordArray = [NSMutableArray array];
    
    while ([rs next]) {
        
        YNTwoPeopleChat * twoPeopleChat = [[YNTwoPeopleChat alloc] init];
        if (![YNIMTool isEmptyDictionary:[rs resultDictionary]]) {
            [twoPeopleChat setValuesForKeysWithDictionary:[rs resultDictionary]];
        }
        
        [twoPeopleChatByKeywordArray addObject:twoPeopleChat];
    }
    [rs close];
    
    return twoPeopleChatByKeywordArray;
}

/**
 用于消息漫游判断
 查询最近多少条信息
 
 @param UserNo 聊天对方账号
 @param sendTime 这个时间之前的多少条数据，nil：最近多少条数据
 @param numberCount 查询多少条数据
 @param db db
 @return 返回结果
 */
+(NSArray *)queryTwoPeopleChatTopInfoWithUserNo:(NSString *)UserNo mTime:(NSString *)mTime numberCount:(NSInteger)numberCount Database:(FMDatabase *)db
{
    NSString *queryTwoPeopleChatByKeywordSql;
    //sendTime为空，获取最近numberCount条数据
    if([YNIMTool isEmptyString:mTime])
    {
        queryTwoPeopleChatByKeywordSql = [NSString stringWithFormat:@"SELECT * FROM  %@ WHERE %@ = '%@' ORDER BY %@ DESC limit %ld",[YNTwoPeopleChat tableName],[YNTwoPeopleChat chatUserNo],UserNo,[YNTwoPeopleChat mTime],numberCount];
    }
    //sendTime有值，获取sendTime时间之前numberCount条数据
    else
    {
        queryTwoPeopleChatByKeywordSql = [NSString stringWithFormat:@"SELECT * FROM  %@ WHERE %@ = '%@' AND (%@ < datetime('%@') OR %@ = '%@') ORDER BY %@ DESC limit %ld",[YNTwoPeopleChat tableName],[YNTwoPeopleChat chatUserNo],UserNo,[YNTwoPeopleChat mTime],mTime,[YNTwoPeopleChat mTime],mTime,[YNTwoPeopleChat mTime],numberCount];
    }
    
    FMResultSet *rs = [db executeQuery:queryTwoPeopleChatByKeywordSql];
    NSMutableArray *twoPeopleChatByKeywordArray = [NSMutableArray array];
    while ([rs next]) {
        YNTwoPeopleChat * twoPeopleChat = [[YNTwoPeopleChat alloc] init];
        if (![YNIMTool isEmptyDictionary:[rs resultDictionary]]) {
            [twoPeopleChat setValuesForKeysWithDictionary:[rs resultDictionary]];
            [twoPeopleChatByKeywordArray addObject:twoPeopleChat];
        }
    }
    [rs close];
    
    return twoPeopleChatByKeywordArray;
}

/**
 查询对应未读消息的第一条消息
 
 @param userNo 聊天信息id
 @param unReadCount 未读条数
 @param db db
 @return 返回数据
 */
+(NSArray *)queryUnReadFirstTwoPeopleChatMessageWithUserNo:(NSString *)userNo unReadCount:(NSInteger)unReadCount inDatabase:(FMDatabase *)db
{
    NSString *OrderByStr = [NSString stringWithFormat:@" ORDER BY %@ DESC",[YNTwoPeopleChat mTime]];
    NSString *queryTwoPeopleChatSql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE  %@ = '%@' %@ LIMIT 0, %ld",
                                       [YNTwoPeopleChat tableName],
                                       [YNTwoPeopleChat chatUserNo],
                                       userNo,
                                       OrderByStr,
                                       unReadCount];
    
    FMResultSet *rs = [db executeQuery:queryTwoPeopleChatSql];
    NSMutableArray *twoPeopleChatByKeywordArray = [NSMutableArray array];
    
    while ([rs next]) {
        
        YNTwoPeopleChat * twoPeopleChat = [[YNTwoPeopleChat alloc] init];
        if (![YNIMTool isEmptyDictionary:[rs resultDictionary]]) {
            [twoPeopleChat setValuesForKeysWithDictionary:[rs resultDictionary]];
        }
        
        [twoPeopleChatByKeywordArray addObject:twoPeopleChat];
    }
    [rs close];
    
    return twoPeopleChatByKeywordArray;
    
}


//更新会话
+ (BOOL)updateTwoPeopleChatTableWithMessageID:(NSString *)messageID sendTime:(NSString *)sendTime isSuccess:(int)messageState inDatabase:(FMDatabase *)db
{
    NSString *updateTwoPeopleChatSql = [NSString stringWithFormat:@"UPDATE %@ SET %@ = '%@', %@ = %@ WHERE %@ = '%@'",
                                        [YNTwoPeopleChat tableName],
                                        [YNTwoPeopleChat mTime],
                                        sendTime,
                                        [YNTwoPeopleChat isSuccess],
                                        [NSNumber numberWithInt:messageState],
                                        [YNTwoPeopleChat packId],
                                        messageID];
    
    BOOL result = [db executeUpdate:updateTwoPeopleChatSql];
    return  result;
}


/**
 JC修改消息漫游
 
 @param packId id
 @param messageContinuousKey key
 @param db db
 @return 返回结果
 */
+(BOOL)updateTwoPeopleChatMessageContinuousTableWithPackId:(NSString *)packId messageContinuousKey:(NSString *)messageContinuousKey isMessageWithdraw:(BOOL)isMessageWithdraw inDatabase:(FMDatabase *)db
{
    NSString *updateTwoPeopleChatSql = [NSString stringWithFormat:@"UPDATE %@ SET %@ = '%@', %@ = %@,%@ = %@ WHERE %@ = '%@'",
                                        [YNTwoPeopleChat tableName],
                                        [YNTwoPeopleChat messageContinuousKey],
                                        messageContinuousKey,
                                        [YNTwoPeopleChat isMessageContinuous],
                                        [NSNumber numberWithInt:YES],
                                        [YNTwoPeopleChat isMessageWithdraw],
                                        [NSNumber numberWithBool:isMessageWithdraw],
                                        [YNTwoPeopleChat packId],
                                        packId];
    
    BOOL result = [db executeUpdate:updateTwoPeopleChatSql];
    return  result;
}




/**
 JC修改所有消息漫游信息
 
 @param messageContinuousKey 新的key
 @param oldMessageContinuousKey 旧的key
 @param db db
 @return 返回结果
 */
+(BOOL)updateTwoPeopleChatAllMessageContinuousWithMessageContinuousKey:(NSString *)messageContinuousKey oldMessageContinuousKey:(NSString *)oldMessageContinuousKey inDatabase:(FMDatabase *)db
{
    NSString *updateTwoPeopleChatSql = [NSString stringWithFormat:@"UPDATE %@ SET %@ = '%@' WHERE %@ = '%@' AND %@ = %@",
                                        [YNTwoPeopleChat tableName],
                                        [YNTwoPeopleChat messageContinuousKey],
                                        messageContinuousKey,
                                        [YNTwoPeopleChat messageContinuousKey],
                                        oldMessageContinuousKey,
                                        [YNTwoPeopleChat isMessageContinuous],
                                        [NSNumber numberWithInt:YES]];
    
    BOOL result = [db executeUpdate:updateTwoPeopleChatSql];
    return  result;
}

/**
 *  修改撤回状态  2018.11.27jc添加
 *
 *  @param packId id
 *  @param db     db
 *
 *  @return 返回状态
 */
+(BOOL)updateTwoPeopleChatWithdrawWithPackId:(NSString *)packId inDatabase:(FMDatabase *)db
{
    NSString *updateTwoPeopleChatSql = [NSString stringWithFormat:@"UPDATE %@ SET %@ = '%@' WHERE %@ = '%@'",
                                        [YNTwoPeopleChat tableName],
                                        [YNTwoPeopleChat isMessageWithdraw],
                                        [NSNumber numberWithBool:YES],
                                        [YNTwoPeopleChat packId],
                                        packId];
    
    BOOL result = [db executeUpdate:updateTwoPeopleChatSql];
    return  result;
}


/**
 *  更新语音状态  2016.8.1jc添加
 *
 *  @param messageID messageID
 *  @param db        db
 *
 *  @return 返回状态
 */
+(BOOL)updateTwoPeopleChatVoiceStateWithMessageID:(NSString *)messageID inDatabase:(FMDatabase *)db
{
    NSString *updateTwoPeopleChatSql = [NSString stringWithFormat:@"UPDATE %@ SET %@ = '1' WHERE %@ = '%@'",
                                        [YNTwoPeopleChat tableName],
                                        [YNTwoPeopleChat isReaded],
                                        [YNTwoPeopleChat packId],
                                        messageID];
    
    BOOL result = [db executeUpdate:updateTwoPeopleChatSql];
    return  result;
}


+ (BOOL)updateTwoPeopleChatTableWithMessageID:(NSString *)messageID messageHeight:(CGFloat)messageHeight inDatabase:(FMDatabase *)db
{
    NSString *updateTwoPeopleChatSql = [NSString stringWithFormat:@"UPDATE %@ SET %@ = %@ WHERE %@ = '%@'",
                                        [YNTwoPeopleChat tableName],
                                        [YNTwoPeopleChat messageHeight],
                                        [NSNumber numberWithFloat:messageHeight],
                                        [YNTwoPeopleChat packId],
                                        messageID];
    
    BOOL result = [db executeUpdate:updateTwoPeopleChatSql];
    return  result;
}

//只更新状态
+ (BOOL)updateMessageSendingStateWithDatabase:(FMDatabase *)db
{
    NSString *updateMessageSendingStateSql = [NSString stringWithFormat:@"UPDATE %@ SET %@ = %@ WHERE %@ = %@",
                                              [YNTwoPeopleChat tableName],
                                              [YNTwoPeopleChat isSuccess],
                                              [NSNumber numberWithInt:YNMessageSendStateFailed],
                                              [YNTwoPeopleChat isSuccess],
                                              [NSNumber numberWithInt:YNMessageSendStateSending]];
    BOOL result = [db executeUpdate:updateMessageSendingStateSql];
    return result;
}

//根据消息的ID清除数据库里面对应的消息
+ (BOOL)deleteAMessageFromDatabaseWithMessageID:(NSString *)messageID inDatabase:(FMDatabase *)db
{
    NSString *deleteMessageSql = [NSString stringWithFormat:@"DELETE FROM %@ WHERE %@ = ?",
                                  [YNTwoPeopleChat tableName],
                                  [YNTwoPeopleChat packId]];
    BOOL result = [db executeUpdate:deleteMessageSql,messageID];
    return result;
}

//更新消息的图片名字, 原图路径, 消息高度
+ (BOOL)updateMessageWithMessageID:(NSString *)messageID
                    originPhotoUrl:(NSString *)originUrl
                          fileName:(NSString *)fileName
                     messageHeight:(CGFloat)messageHeight
                        inDatabase:(FMDatabase *)db
{
    NSString *updateMessage = [NSString stringWithFormat:@"UPDATE %@ SET %@ = '%@', %@ = '%@', %@ = %@ WHERE %@ = '%@'",
                               [YNTwoPeopleChat tableName],
                               [YNTwoPeopleChat originPhotoUrl],
                               originUrl,
                               [YNTwoPeopleChat fileName],
                               fileName,
                               [YNTwoPeopleChat messageHeight],
                               [NSNumber numberWithFloat:messageHeight],
                               [YNTwoPeopleChat packId], messageID];
    BOOL result = [db executeUpdate:updateMessage];
    
    return result;
}

//更改文件状态1
+ (BOOL)updateMessageFileStateWithMessageID:(NSString *)messageID fileState:(NSInteger)fileState inDataBase:(FMDatabase *)db
{
    NSString *updateMsg = [NSString stringWithFormat:@"UPDATE %@ SET %@ = %@ WHERE %@ = '%@'",
                           [YNTwoPeopleChat tableName],
                           [YNTwoPeopleChat fileState],
                           [NSNumber numberWithInteger:fileState],
                           [YNTwoPeopleChat packId],
                           messageID];
    BOOL result = [db executeUpdate:updateMsg];
    
    return result;
}
@end
